package com.ruoyi.pom.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.pom.domain.PomTasks;
import com.ruoyi.pom.mapper.PomSchedulesMapper;
import com.ruoyi.pom.domain.PomSchedules;
import com.ruoyi.pom.service.IPomSchedulesService;

/**
 * schedulesService业务层处理
 * 
 * @author ruoyi
 * @date 2025-01-10
 */
@Service
public class PomSchedulesServiceImpl extends  ServiceImpl<PomSchedulesMapper, PomSchedules> implements IPomSchedulesService
{
    @Autowired
    private PomSchedulesMapper pomSchedulesMapper;

    /**
     * 查询schedules
     * 
     * @param id schedules主键
     * @return schedules
     */
    @Override
    public PomSchedules selectPomSchedulesById(Long id)
    {
        return pomSchedulesMapper.selectPomSchedulesById(id);
    }

    /**
     * 查询schedules列表
     * 
     * @param pomSchedules schedules
     * @return schedules
     */
    @Override
    public List<PomSchedules> selectPomSchedulesList(PomSchedules pomSchedules)
    {
        return pomSchedulesMapper.selectPomSchedulesList(pomSchedules);
    }

    /**
     * 新增schedules
     * 
     * @param pomSchedules schedules
     * @return 结果
     */
    @Transactional
    @Override
    public int insertPomSchedules(PomSchedules pomSchedules)
    {
        pomSchedules.setCreateTime(DateUtils.getNowDate());
        int rows = pomSchedulesMapper.insertPomSchedules(pomSchedules);
        insertPomTasks(pomSchedules);
        return rows;
    }

    /**
     * 修改schedules
     * 
     * @param pomSchedules schedules
     * @return 结果
     */
    @Transactional
    @Override
    public int updatePomSchedules(PomSchedules pomSchedules)
    {
        pomSchedules.setUpdateTime(DateUtils.getNowDate());
        pomSchedulesMapper.deletePomTasksBySchedulesId(pomSchedules.getId());
        insertPomTasks(pomSchedules);
        return pomSchedulesMapper.updatePomSchedules(pomSchedules);
    }

    /**
     * 批量删除schedules
     * 
     * @param ids 需要删除的schedules主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePomSchedulesByIds(Long[] ids)
    {
        pomSchedulesMapper.deletePomTasksBySchedulesIds(ids);
        return pomSchedulesMapper.deletePomSchedulesByIds(ids);
    }

    /**
     * 删除schedules信息
     * 
     * @param id schedules主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePomSchedulesById(Long id)
    {
        pomSchedulesMapper.deletePomTasksBySchedulesId(id);
        return pomSchedulesMapper.deletePomSchedulesById(id);
    }

    /**
     * 新增tasks信息
     * 
     * @param pomSchedules schedules对象
     */
    public void insertPomTasks(PomSchedules pomSchedules)
    {
        List<PomTasks> pomTasksList = pomSchedules.getPomTasksList();
        Long id = pomSchedules.getId();
        if (StringUtils.isNotNull(pomTasksList))
        {
            List<PomTasks> list = new ArrayList<PomTasks>();
            for (PomTasks pomTasks : pomTasksList)
            {
                pomTasks.setSchedulesId(id);
                list.add(pomTasks);
            }
            if (list.size() > 0)
            {
                pomSchedulesMapper.batchPomTasks(list);
            }
        }
    }
}
